// 新增研发饱和度指标组件


// 新增团队情况组件


import React, { Component } from 'react';
import NetUitl from '../utils/HttpUtil';
import {Input, message, Table} from "antd";
import TitleBar from "./TitleBar";
import "./add.scss";


class AddSaturation extends Component {
    constructor(props) {
        super(props);
        this.state = {
            dataSource:[
                {
                    codeNum:"",
                    updateDemandNum:"",
                    updateBugNum:0,
                    codeAvg:"",
                    updateDemandAvg:"",
                    updateBugAvg:0
                }
            ]
        }

        this.columns=[
            {
                title: '当日代码行数',
                dataIndex: 'codeNum',
                width:"16.6%",
                render: (text,record)=>{
                    if (this.props.isAdd === true){
                        return <Input placeholder={"必填：数字"} value={record.codeNum} onChange={this.codeChange}></Input>
                    }else{
                        return <span>{text}</span>
                    }
                }
            },
            {
                title: '当日需求及BUG修改数',
                dataIndex: 'updateDemandNum',
                width:"16.6%",
                render: (text,record)=>{
                    if (this.props.isAdd === true){
                        return <Input placeholder={"必填：数字"} value={record.updateDemandNum}  onChange={this.needChange}></Input>
                    }else{
                        return <span>{text}</span>
                    }
                }
            },
            // {
            //     title: '当日修改BUG数',
            //     dataIndex: 'updateBugNum',
            //     width:"16.6%",
            //     render: (text,record)=>{
            //         if (this.props.isAdd === true){
            //             return <Input placeholder={"必填：数字"} value={record.updateBugNum}   onChange={this.bugChange}></Input>
            //         }else{
            //             return <span>{text}</span>
            //         }
            //     }
            // },
            {
                title: '当日人均代码行数',
                dataIndex: 'codeAvg',
                width:"16.6%",
                render: (text,record)=>{
                    if (this.props.isAdd === true){
                        return <Input value={record.codeAvg}  onChange={this.codeAvgChange}></Input>
                    }else{
                        return <span>{text}</span>
                    }
                }
            },
            {
                title: '当日人均修改需求及BUG数',
                dataIndex: 'updateDemandAvg',
                width:"16.6%",
                render: (text,record)=>{
                    if (this.props.isAdd === true){
                        return <Input value={record.updateDemandAvg}  onChange={this.updateDemandAvgChange}></Input>
                    }else{
                        return <span>{text}</span>
                    }
                }
            },
            // {
            //     title: '当日人均修改BUG数',
            //     dataIndex: 'updateBugAvg',
            //     width:"16.6%",
            // },
        ]
    }

    componentDidMount(){
        this.publicFn(this.props)
    }
    componentWillReceiveProps(nextprops){
        this.publicFn(nextprops)
    }

    //接收父组件传的数据
    publicFn(nextprops){
        let dataSource=[];
        let sat = nextprops.allData &&  nextprops.allData.sat?nextprops.allData.sat:""
        let {id,date, stateType} = nextprops
        dataSource.push(sat);
        console.log(nextprops,"componentWillReceiveProps")
        this.setState({dataSource:dataSource,id:id,date:date,stateType:stateType})
    }

    //当日代码行数change事件
    codeChange=(e)=>{
        let team = this.props.allData.team;
        if (team.attendanceNum === "" || team.attendanceNum === undefined || team.attendanceNum === null){
            return  message.warning("请先填写出勤人数！")
        }

        var re =  /^(0|\+?[1-9][0-9]*)$/;
        if (e.target.value !== ""){
            if (!re.test( e.target.value)) {
                return  message.warning("请输入正整数！");
            }
        }

        let {dataSource} = this.state;
        let codeNum = e.target.value;
        let out=this.props.allData.team.attendanceNum;
        let avgCode = codeNum / out;
        dataSource[0].codeNum = codeNum;
        dataSource[0].codeAvg = avgCode.toFixed(2);
        this.setState({dataSource});

        // let allData={};
        // allData.sat = dataSource[0];
        // this.props.viewer.setState({allData})
    }

    //当日修改需求行数change事件
    needChange=(e)=>{
        let team = this.props.allData.team;
        if (team.attendanceNum === "" || team.attendanceNum === undefined || team.attendanceNum === null){
            return  message.warning("请先填写出勤人数！")
        }

        var re =  /^(0|\+?[1-9][0-9]*)$/;   //正整数
        if (e.target.value !== ""){
            let value=e.target.value;
            let index = value.indexOf(".");
            if (index !== -1){
                value=value.substring(0,index);
            }
            if (!re.test( value)) {
                return  message.warning("请输入正整数或正小数！");
            }
        }

        let {dataSource} = this.state;
        let needNum = e.target.value;
        let out=this.props.allData.team.attendanceNum;
        let avgNeeds = needNum / out;
        dataSource[0].updateDemandNum = needNum;
        dataSource[0].updateDemandAvg = avgNeeds.toFixed(2);
        this.setState({dataSource});

        // let allData={};
        // allData.sat = dataSource[0];
        // this.props.viewer.setState({allData})
    }

    //当日修改bug行数change事件
    bugChange=(e)=>{
        let team = this.props.allData.team;
        if (team.attendanceNum === "" || team.attendanceNum === undefined || team.attendanceNum === null){
            return  message.warning("请先填写出勤人数！")
        }

        var re =  /^(0|\+?[1-9][0-9]*)$/;   //正整数
        if (e.target.value !== ""){
            let value=e.target.value;
            let index = value.indexOf(".");
            if (index !== -1){
                value=value.substring(0,index);
            }

            if (!re.test( value)) {
                return  message.warning("请输入正整数或正小数！");
            }
        }

        let {dataSource} = this.state;
        let bugNum = e.target.value;
        let out=this.props.allData.team.attendanceNum;
        let avgBug = bugNum / out;
        dataSource[0].updateBugNum = bugNum;
        dataSource[0].updateBugAvg = avgBug.toFixed(2);
        this.setState({dataSource});

        // let allData={};
        // allData.sat = dataSource[0];
        // this.props.viewer.setState({allData})
    }
    //修改当日人均代码行数
    codeAvgChange = (e) =>{
        let {dataSource} = this.state;
        dataSource[0].codeAvg = e.target.value;
        this.setState({dataSource});
    }
    // 修改当日人均修改需求及BUG数
    updateDemandAvgChange = (e) =>{
        let {dataSource} = this.state;
        dataSource[0].updateDemandAvg = e.target.value;
        this.setState({dataSource});
    }


    render() {
        const isAdd = this.props.isAdd;

        return (
            <div className="addSat-box">
                <div className={"addSat-top"}>
                    <div>
                        <TitleBar name={"研发饱和度指标"}></TitleBar>
                    </div>
                </div>
                <div className={"addSat-main"}>
                    <Table
                        className={"addSat-table"}
                        bordered
                        columns={this.columns}
                        dataSource={this.state.dataSource}
                        pagination={false}
                    />
                </div>
            </div>
        );
    }
}

export default  AddSaturation;
